#define _CRT_SECURE_NO_WARNINGS  1

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#define MAX_BOOKS 100
#define MAX_READERS 100

typedef struct {
    char title[50];
    char author[50];
    char publisher[50];
    char publicationYear[50];
    int stock;
} Book;

typedef struct {
    char name[50];
    char address[100];
    int booksBorrowed;
} Reader;

typedef struct {
    char bookTitle[50];
    char borrowerName[50];
    char borrowDate[20];
    char returnDate[20];
} BorrowInfo;

// 借阅列表
BorrowInfo borrowRecords[MAX_BOOKS];
// 图书列表
Book books[MAX_BOOKS];
// 读者列表
Reader readers[MAX_READERS];

// 图书数量
int numBooks = 0;
// 读者数量
int numReaders = 0;
// 借阅数量
int numBorrowRecords = 0;

// 以上三个变量都表示图书馆内的统计信息，可以考虑放在一个 struct 中
//typedef struct {
//    int numbBooks;
//    int numReaders;
//    int numBorrowRecords;
//} LibraryInfo;

/**
 * 退出系统
 */
void exitSystem() {
    printf("感谢使用图书管理系统,再见.\n");
    exit(0);
}

/**
 * 添加图书
 * 依次按照提示输入相应的信息并保存
 */
void addBook() {
    Book book;
    if (numBooks >= MAX_BOOKS) {
        printf("没有更多的空间添加新图书.\n");
        return;
    }
    printf("请输入图书书名: ");
    scanf("%s", book.title);

    printf("请输入图书作者: ");
    scanf("%s", book.author);

    printf("请输入图书出版单位: ");
    scanf("%s", book.publisher);

    printf("请输入图书出版时间: ");
    scanf("%s", book.publicationYear);

    printf("请输入图书库存数量: ");
    scanf("%d", &book.stock);

    books[numBooks++] = book;
    printf("图书添加成功.\n");
}

/**
 * 展示所有图书
 */
void displayBooks() {
    int i;

    if (numBooks == 0) {
        printf("没有可展示的图书.\n");
        return;
    }

    printf("图书:\n");
    for (i = 0; i < numBooks; i++) {
        printf("书名: %s\n", books[i].title);
        printf("作者: %s\n", books[i].author);
        printf("出版单位: %s\n", books[i].publisher);
        printf("出版时间: %s\n", books[i].publicationYear);
        printf("库存: %d\n", books[i].stock);
        printf("-----------------\n");
    }
}

/**
 * 搜索图书
 */
void searchBooks() {
    char query[50];
    int i;

    if (numBooks == 0) {
        printf("没有可被搜索的图书.\n");
        return;
    }

    printf("请输入图书任一相关信息（书名、作者、出版单位、出版时间）: ");
    scanf("%s", query);

    printf("搜索结果:\n");
    for (i = 0; i < numBooks; i++) {
        if (strstr(books[i].title, query) != NULL ||
            strstr(books[i].author, query) != NULL ||
            strstr(books[i].publisher, query) != NULL ||
            strstr(books[i].publicationYear, query) != NULL) {

            printf("书名: %s\n", books[i].title);
            printf("作者: %s\n", books[i].author);
            printf("出版单位: %s\n", books[i].publisher);
            printf("出版时间: %s\n", books[i].publicationYear);
            printf("库存: %d\n", books[i].stock);
            printf("-----------------\n");
        }
    }
}

/**
 * 添加读者
 */
void addReader() {
    Reader reader;

    // TODO 我认为这里应该判断大于即可
    if (numReaders >= MAX_READERS) {
        printf("没有更多的空间来添加新的读者.\n");
        return;
    }

    printf("请输入读者姓名: ");
    scanf("%s", reader.name);

    printf("请输入读者邮件地址: ");
    scanf("%s", reader.address);

    reader.booksBorrowed = 0;

    readers[numReaders++] = reader;
    printf("读者添加成功.\n");
}

/**
 * 借书
 */
void borrowBook() {
    struct tm *currentDate;

    Reader *reader = NULL;
    Book *book = NULL;

    char readerName[50];
    char bookTitle[50];

    int i;
    time_t t;

    if (numBooks == 0) {
        printf("没有可被借阅的图书.\n");
        return;
    }

    if (numReaders == 0) {
        printf("没有可借书的读者.\n");
        return;
    }

    printf("请输入读者姓名: ");
    scanf("%s", readerName);
    for (i = 0; i < numReaders; i++) {
        if (strcmp(readers[i].name, readerName) == 0) {
            reader = &readers[i];
            break;
        }
    }

    if (reader == NULL) {
        printf("没有找到该读者.\n");
        return;
    }

    printf("请输入书名: ");
    scanf("%s", bookTitle);
    for (i = 0; i < numBooks; i++) {
        if (strcmp(books[i].title, bookTitle) == 0) {
            book = &books[i];
            break;
        }
    }
    if (book == NULL) {
        printf("没有找到该图书.\n");
        return;
    }
    if (book->stock > 0 && reader->booksBorrowed < 3) {
        book->stock--;
        reader->booksBorrowed++;
        printf("图书借阅成功.\n");
    } else {
        printf("图书不存在或已达到借阅上限.\n");
    }

    strcpy(borrowRecords[numBorrowRecords].bookTitle, bookTitle);
    strcpy(borrowRecords[numBorrowRecords].borrowerName, readerName);
    time(&t);
    currentDate = localtime(&t);
    strftime(borrowRecords[numBorrowRecords].borrowDate, sizeof(borrowRecords[numBorrowRecords].borrowDate), "%Y-%m-%d",
             currentDate);
    numBorrowRecords++;
}

/**
 * 归还图书
 */
void returnBook() {
    Reader *reader = NULL;
    Book *book = NULL;

    char readerName[50];
    char bookTitle[50];

    int i = 0, j = 0;

    if (numBooks == 0) {
        printf("没有需要归还的图书.\n");
        return;
    }

    if (numReaders == 0) {
        printf("没有读者需要还书.\n");
        return;
    }

    printf("请输入读者姓名: ");
    scanf("%s", readerName);
    for (i = 0; i < numReaders; i++) {
        if (strcmp(readers[i].name, readerName) == 0) {
            reader = &readers[i];
            break;
        }
    }
    if (reader == NULL) {
        printf("没有找到该读者.\n");
        return;
    }

    printf("请输入书名: ");
    scanf("%s", bookTitle);
    for (i = 0; i < numBooks; i++) {
        if (strcmp(books[i].title, bookTitle) == 0) {
            book = &books[i];
            break;
        }
    }
    if (book == NULL) {
        printf("没有找到该图书.\n");
        return;
    }

    if (reader->booksBorrowed > 0) {
        book->stock++;
        reader->booksBorrowed--;
        printf("图书归还成功.\n");
    } else {
        printf("该读者没有需要归还的图书.\n");
    }

    for (i = 0; i < numBorrowRecords; i++) {
        if (strcmp(borrowRecords[i].bookTitle, bookTitle) == 0 &&
            strcmp(borrowRecords[i].borrowerName, readerName) == 0) {
            for (j = i; j < numBorrowRecords - 1; j++) {
                strcpy(borrowRecords[j].bookTitle, borrowRecords[j + 1].bookTitle);
                strcpy(borrowRecords[j].borrowerName, borrowRecords[j + 1].borrowerName);
            }
            numBorrowRecords--;
            break;
        }
    }
}

/**
 * 查看借阅记录
 */
void searchBorrowRecords() {
    char borrowerName[50];
    int found = 0, i = 0;

    printf("请输入读者姓名: ");
    scanf("%s", borrowerName);

    printf("=== 借阅记录 ===\n");
    for (i = 0; i < numBorrowRecords; i++) {
        if (strcmp(borrowRecords[i].borrowerName, borrowerName) == 0) {
            printf("书名: %s\n", borrowRecords[i].bookTitle);
            printf("借阅者: %s\n", borrowRecords[i].borrowerName);
            printf("借阅日期: %s\n", borrowRecords[i].borrowDate);
            printf("\n");
            found = 1;
        }
    }
    if (!found) {
        printf("没有查到 %s的借阅信息.\n", borrowerName);
    }
}

/**
 * 更新读者信息
 */
void updateReader() {
    Reader *reader = NULL;

    char readerName[50];

    int i = 0;

    printf("请输入读者姓名: ");
    scanf("%s", readerName);
    for (i = 0; i < numReaders; i++) {
        if (strcmp(readers[i].name, readerName) == 0) {
            reader = &readers[i];
            break;
        }
    }
    if (reader == NULL) {
        printf("没有找到该读者.\n");
        return;
    }

    printf("请输入新的邮件地址: ");
    scanf("%s", reader->address);
    printf("读者信息更新成功.\n");
}

/**
 * 查找读者信息
 */
void searchReader() {
    Reader *reader = NULL;

    char readerName[50];

    int i = 0;

    printf("请输入读者姓名: ");
    scanf("%s", readerName);
    for (i = 0; i < numReaders; i++) {
        if (strcmp(readers[i].name, readerName) == 0) {
            reader = &readers[i];
            break;
        }
    }
    if (reader == NULL) {
        printf("没有找到该读者.\n");
        return;
    }

    printf("读者信息:\n");
    printf("姓名: %s\n", reader->name);
    printf("邮件地址: %s\n", reader->address);
    printf("图书借阅数量: %d\n", reader->booksBorrowed);
}

/**
* 更新图书信息
*/
void updateBook() {
    Book *book = NULL;

    char title[50];

    int i = 0;

    if (numBooks == 0) {
        printf("没有可更改的图书.\n");
        return;
    }

    printf("请输入要更新信息的图书: ");
    scanf("%s", title);
    for (i = 0; i < numBooks; i++) {
        if (strcmp(books[i].title, title) == 0) {
            book = &books[i];
            break;
        }
    }
    if (book == NULL) {
        printf("没有找到图书.\n");
        return;
    }

    printf("请输入图书作者: ");
    scanf("%s", book->author);

    printf("请输入图书出版单位: ");
    scanf("%s", book->publisher);

    printf("请输入图书出版时间: ");
    scanf("%s", book->publicationYear);

    printf("请输入图书库存数量: ");
    scanf("%d", &(book->stock));

    printf("图书信息更新成功.\n");
}

/**
 * 展示菜单
 */
void displayMenu() {
    printf("\n ==== 菜单 ==== \n");
    printf("1. 添加图书\n");
    printf("2. 展示全部图书\n");
    printf("3. 寻找图书\n");
    printf("4. 添加读者\n");
    printf("5. 借阅图书\n");
    printf("6. 归还图书\n");
    printf("7. 查找借阅记录\n");
    printf("8. 更改读者信息\n");
    printf("9. 查找读者信息\n");
    printf("10.修改图书信息\n");
    printf("0. 退出\n");
}

// 根据实际情况添加函数，顺序请与 Menu 中定义的顺序相同
void (*funcArr[])() =
        {
                exitSystem,
                addBook,
                displayBooks,
                searchBooks,
                addReader,
                borrowBook,
                returnBook,
                searchBorrowRecords,
                updateReader,
                searchReader,
                updateBook
        };

int main() {
    int choice;

    printf("欢迎使用图书管理系统!\n");
    do {
        displayMenu();
        printf("请选择操作: ");
        scanf("%d", &choice);

        if (choice < 0 || choice > sizeof(funcArr)) {
            printf("无效操作，请重新选择\n");
            continue;
        }

        funcArr[choice]();
    } while (choice != 0);
    return 0;
}